var lcff_promptSave = {
    existingNames : [],
    fields : [],
    
    /**
      * Fills the 'suggested name' textbox, sets up the fields to save for this account
      */
    onLoad : function() {
        var params = window.arguments[0].inparams;
dump("P"+params+"\n");
        LockCrypt.loadPrefs();
        document.getElementById('txtAccountName').value = decodeURIComponent(params.suggested);
        lcff_promptSave.existingNames = params.existing;
        document.getElementById('txtAccountName').focus();
        lcff_promptSave.fields = params.fields;
        if(params.isChanged) {
            document.getElementById('saveTitle').value = LockCrypt.getString('accountChanged');
        }
    },

    /**
      * Checks the account name entered does not already exist, saves data to window.arguments[0].out, then closes the dialog
      */
    validateName : function() {
        var name = document.getElementById('txtAccountName').value;
        if(LockCrypt.arrayContains(name, this.existingNames)) {
            var prompts = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
                                    .getService(Components.interfaces.nsIPromptService);
            var flags = prompts.STD_YES_NO_BUTTONS;
            var overwrite = prompts.confirmEx(window, LockCrypt.getString("warning"), LockCrypt.getString("err_AccountExistsOverwrite"), flags, 
                         null,null,null,null,{}) == 0 ? true : false;
            if(overwrite) {
                window.arguments[0].out = {
                    name : document.getElementById('txtAccountName').value,
                    fields : lcff_promptSave.fields,
                    thisURL : document.getElementById('chkThisURL').checked
                };
            } else {
                document.getElementById('txtAccountName').focus();
                return;
            }
        } else {
            window.arguments[0].out = {
                name : document.getElementById('txtAccountName').value,
                fields : lcff_promptSave.fields,
                thisURL : document.getElementById('chkThisURL').checked
            };
        }
        close();
    },
    
    /**
      * Shows a dialog to edit fields to save
      */
    toggleSaveOptions : function() {
        var params = {
            fieldsin : lcff_promptSave.fields,
            fieldsout : null
        };
        window.openDialog("chrome://LockCrypt/content/promptsave_options.xul", "promptsave_options", "modal,centerscreen,chrome", params);
        lcff_promptSave.fields = params.fieldsout;
    },
    
    /**
      * Closes the dialog
      */
    cancelDialog : function() {
        close();
    },
    
    /**
      * Closes the dialog and excludes the site to prevent re-prompting to save
      */
    cancelAndExclude : function() {
        LockCrypt.excludeSite(window.arguments[0].inparams.url); 
        close();
    }
};